package com.gangwangchina.android.contactlib.model;


import java.util.HashMap;
import java.util.Map;

import com.gangwangchina.android.contactlib.ContactsInterface;

import android.provider.ContactsContract.CommonDataKinds.StructuredName;



public class PersonName extends DataElement{
	
	public static final String DISPLAY_NAME = StructuredName.DISPLAY_NAME;
	public static final String GIVEN_NAME = StructuredName.GIVEN_NAME;
	public static final String FAMILY_NAME = StructuredName.FAMILY_NAME;
	public static final String PREFIX = StructuredName.PREFIX;
	public static final String MIDDLE_NAME = StructuredName.MIDDLE_NAME;
	public static final String SUFFIX = StructuredName.SUFFIX;
	public static final String CONTENT_ITEM_TYPE = StructuredName.CONTENT_ITEM_TYPE;
	
	public String givenName = "";
	public String familyName = "";
	public String prefix = "";
	public String middleName = "";
	public String suffix = "";
	
	/**
	 * Creates a new PersonName that is not (yet) present in the database
	 * @param givenName
	 * @param familyName
	 * @param prefix
	 * @param middleName
	 * @param suffix
	 * @return
	 */
	public static PersonName newPersonName(String givenName, String familyName, String prefix, String middleName, String suffix){
		return new PersonName(givenName, familyName, prefix, middleName, suffix);
	}
	
	/**
	 * Creates an object representation of a PersonName that is present in the database
	 * @param pcId
	 * @param givenName
	 * @param familyName
	 * @param prefix
	 * @param middleName
	 * @param suffix
	 * @return
	 */
	public static PersonName recoverPersonName(long pcId, String givenName, String familyName, String prefix, String middleName, String suffix){
		return new PersonName(pcId, givenName, familyName, prefix, middleName, suffix);
	}
	
	/**
	 * Creates a new PersonName
	 * @param pcId the PersonContact the PersonName is linked to
	 * @param givenName
	 * @param familyName
	 * @param prefix
	 * @param middleName
	 * @param suffix
	 */
	private PersonName(long pcId, String givenName, String familyName, String prefix, String middleName, String suffix){
		this(pcId);
		this.givenName = givenName;
		this.familyName = familyName;
		this.middleName = middleName;
		this.prefix = prefix;
		this.suffix = suffix;
	}
	
	private PersonName(String givenName, String familyName, String prefix, String middleName, String suffix){
		this(ContactsInterface.NO_ID, givenName, familyName, prefix, middleName, suffix);
	}
	
	private PersonName(long pcId){
		super(pcId, CONTENT_ITEM_TYPE);
	}
	
	/**
	 * 
	 * @return the displayed name
	 */
	public String displayName(){
		StringBuilder name = new StringBuilder();
		if(givenName!=null) name.append(givenName).append(' ');
		if(middleName!=null) name.append(middleName).append(' ');
		if(familyName!=null) name.append(familyName);
		if(suffix!=null) name.append(", ").append(suffix);
			
		return name.toString();
	}

//	@Override
	public String toString(){
		return displayName();
	}

	
	
	@Override
	public Map<String, Object> getDataColumns() {		
		Map<String, Object> datas = new HashMap<String, Object>();
		datas.put(DISPLAY_NAME, displayName());
		datas.put(GIVEN_NAME, givenName);
		datas.put(FAMILY_NAME, familyName);
		datas.put(MIDDLE_NAME, middleName);
		datas.put(SUFFIX, suffix);
		datas.put(PREFIX, prefix);
		
		return datas;
	}
	
	@Override
	public boolean setProperty(String key, Object value){
		if(key.equals(GIVEN_NAME)) givenName = (String) value;
		else if(key.equals(FAMILY_NAME)) familyName = (String) value;
		else if(key.equals(MIDDLE_NAME)) middleName = (String) value;
		else if(key.equals(PREFIX)) prefix = (String) value;
		else if(key.equals(SUFFIX)) suffix = (String) value;
		else return super.setProperty(key, value);
		return true;
	}
	
	@Override
	public Object getProperty(String key){
		if(key.equals(GIVEN_NAME)) return givenName;
		else if(key.equals(FAMILY_NAME)) return familyName;
		else if(key.equals(MIDDLE_NAME)) return middleName;
		else if(key.equals(PREFIX)) return prefix;
		else if(key.equals(SUFFIX)) return suffix;
		else return super.getProperty(key);
	}
		
}
